<?php ob_start(); ?>
<?php
if ( $_POST ) {
	global $error;
	$error = FALSE;

	//konek ke db
	$con = mysql_connect("localhost","progin","progin");
	if (!$con){
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("progin_171_13509027", $con);
}
?>

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Registration</title>
	<link rel="stylesheet" type="text/css" title="theme1" href="css/theme1.css"/>
</head>

<body>
	<div id="header">
		<div class="header_container">
			<div class="header_grid">
				<div class="logo">
					<h1>
						<a href="index.html" title="">Tyvo Ranger</a>
					</h1>
				</div>
				<div class="navigation">
					<ul class="menu">
						
						<li> <a href='index.php?page=home&state=1'> <span> <span> Home </span> </span> </a> </li>
						<li> <a href='index.php?page=contents&state=1&p=1'> <span> <span> Content </span> </span> </a> </li>
						<li> <a href='index.php?page=home&state=1'> <span> <span> Post </span> </span> </a> </li>
						<li><a href='#'> <span> <span> Login </span> </span> </a></li>
						<li id='current-menu'><a href='register.php'> <span> <span> Register</span> </span> </a></li>
						
					</ul>
				</div>
				<form class="form-search">
					<span class="search-icon">
						<i class="nav-search"></i>
					</span>
					<input class="search-input" type="text" name="q" placeholder="Search">
					<ul id="dropdown">
						<li>
							<a class="search-filter" href="#">
								<i class="nav-search-filter"></i>
							</a>
							<ul>
								<li><a href="#">no filter</a></li>
								<li><a href="#">by username</a></li>
								<li><a href="#">by content</a></li>
							</ul>					
						</li>
					</ul>
				</form>
			</div>	
		</div>
		<div id="header-line"></div>
	</div>
	
	<div class="primaryContent">
		<div class="container">
			<div id="content">
				<h2>Registration</h2>
				<form name="register-form" class="register-form" action="register.php" method="POST" enctype="multipart/form-data">
					<span>Username </span><input type="text" name="username" id="username" <?php if($_POST){$username=$_POST["username"]; echo "value=$username";}?> /><br/><br/>
						<?php
							//validasi username
							if ( $_POST ) {
								if($_POST["username"]=="" || $_POST["username"]==NULL){
									$error = TRUE;
									echo "<i> Username cannot be blank.</i><br/>";
									echo '<style type="text/css"> #username{background-color:#f27b75;} </style>';
								}
								if(strlen($_POST["username"])<5){
									$error = TRUE;
									echo "<i> Username must contain at least 5 characters. </i><br/>";
									echo '<style type="text/css"> #username{background-color:#f27b75;} </style>';
								}
								$username = $_POST["username"];
								$sql = "SELECT count(*) FROM `user` WHERE `username`='$username'";  
								$taken = mysql_result(mysql_query($sql),0);  
								if($taken){ 
									$error = TRUE;
									echo "<i>This username is already used. Please pick another on. </i><br/>"; 
									echo '<style type="text/css"> #username{background-color:#f27b75;} </style>';
								} 
							}
						?>					
					<span>Email </span><input type="text" name="email" id="email" <?php if($_POST){$email=$_POST["email"]; echo "value=$email";}?> /><br/><br/>
						<?php
							//validasi email
							if ( $_POST ) {
								if ($_POST["email"]=="" || $_POST["email"]==NULL) {
									$error = TRUE;
									echo "<i> Email cannot be blank.</i><br/>";
									echo '<style type="text/css"> #email{background-color:#f27b75;} </style>';
								}
								if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i", $_POST["email"])==FALSE) {
									$error = TRUE;
									echo "<i> Not a valid email address. </i><br/>";
									echo '<style type="text/css"> #email{background-color:#f27b75;} </style>';
								}
								$email = $_POST["email"];
								$sql = "SELECT count(*) FROM `user` WHERE `email`='$email'";  
								$taken = mysql_result(mysql_query($sql),0);  
								if($taken){ 
									$error = TRUE;
									echo "<i> This email is already used. Please pick another on. </i><br/>"; 
									echo '<style type="text/css"> #email{background-color:#f27b75;} </style>';
								} 
							}
						?>				
					<span>Password </span><input type="password" name="pwd" id="pwd" <?php if($_POST){$password=$_POST["pwd"]; echo "value=$password";}?> /><br/><br/>
						<?php
							//validasi password
							if ( $_POST ) {
								if($_POST["pwd"]=="" || $_POST["pwd"]==NULL || strlen($_POST["pwd"])<8){
									$error = TRUE;
									echo "<i>Password must contain at least eight characters. </i><br/>";
									echo '<style type="text/css"> #pwd{background-color:#f27b75;} </style>';
								}
								if($_POST["pwd"]==$_POST["username"] || $_POST["pwd"]==$_POST["email"]){
									$error = TRUE;
									echo "<i>Password must be different from username and email. </i><br/>";
									echo '<style type="text/css"> #pwd{background-color:#f27b75;} </style>';
								}
							}
						?>
					<span>Confirm Password </span><input type="password" name="pwd2" id="pwd2" <?php if($_POST){$password2=$_POST["pwd2"]; echo "value=$password2";}?> /><br/><br/>
						<?php
							//validasi confirm password
							if ( $_POST ) {
								if($_POST["pwd2"]!=$_POST["pwd"]){
									$error = TRUE;
									echo "<i>Confirm password must be same with password. </i><br/>";
									echo '<style type="text/css"> #pwd2{background-color:#f27b75;} </style>';
								}
							}
						?>
					<span>Full Name </span><input type="text" name="fullname" id="fullname" <?php if($_POST){$fullname=$_POST["fullname"]; echo "value=$fullname";}?> /><br/><br/>
						<?php
							//validasi fullname
							if ( $_POST ) {
								if ($_POST["fullname"]=="" || $_POST["fullname"]==NULL || 
									preg_match("/\w+\s+\w/i",$_POST["fullname"])==FALSE){
									$error = TRUE;
									echo "<i>Full Name must contain at least two words with space between. </i><br/>";
									echo '<style type="text/css"> #fullname{background-color:#f27b75;} </style>';
								}
							}
						?>
					<span>Date of Birth </span><input type="text" name="date" id="date" placeholder="YYYY-MM-DD" <?php if($_POST){$date=$_POST["date"]; echo "value=$date";}?> /><br/><br/>
						<?php
							//validasi date
							if ( $_POST ) {
								if ($_POST["date"]=="" || $_POST["date"]==NULL ||  
									preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/i",$_POST["date"])==FALSE){
									$error = TRUE;
									echo "<i>Date must be in YYYY-MM-DD. </i><br/>";
									echo '<style type="text/css"> #date{background-color:#f27b75;} </style>';
								} else {
									list($y, $m, $d) = explode("-", $_POST["date"]);
									if (checkdate($m, $d, $y)==FALSE){
										$error = TRUE;
										echo "<i>Date must be Gregorian Date. </i><br/>";
										echo '<style type="text/css"> #date{background-color:#f27b75;} </style>';
									}
								}
							}
						?>
					<span>Avatar </span><input type="file" name="avatar" id="avatar" accept="image/jpeg"/><br/><br/>
						<?php
							//validasi avatar
							if ( $_POST ) {
								if ($_FILES["avatar"]["error"] > 0){
									$error = TRUE;
									echo "<i>Avatar cannot be blank. </i><br />";
									echo '<style type="text/css"> #avatar{background-color:#f27b75;} </style>';
								} else if ($_FILES["avatar"]["type"] != "image/jpeg"){
									$error = TRUE;
									echo "<i>Input image must be jpg or jpeg. </i><br />";
									echo '<style type="text/css"> #avatar{background-color:#f27b75;} </style>';
								}
							}
						?>
					<span>Gender </span><select name="gender" id="gender">
							<option value="L" id="male" <?php if($_POST){if($_POST["gender"]=="L") echo 'selected="selected"';}?> >Male</option>
							<option value="P" id="female" <?php if($_POST){if($_POST["gender"]=="P") echo 'selected="selected"';}?> >Female</option>
							</select><br/><br/><br/>
					<span>About Me </span><textarea name="aboutme" rows="70" cols="200"><?php if($_POST){$aboutme=$_POST["aboutme"]; echo "$aboutme";}?></textarea><br/><br/><br/><br/><br/><br/>
					<input type="submit" name="submitForm" id="submitForm" value="Register"/>
				</form>
			</div>
		</div>
	</div>
</body>

</html>

<?php
if ( $_POST ) {
	if($error==FALSE){
		//masukin ke db kalo udah ga ada yg error
		$username = $_POST["username"];
		$email = $_POST["email"];
		$password = $_POST["pwd"];
		$password2 = $_POST["pwd2"];
		$fullname = ucwords($_POST["fullname"]);
		$date = $_POST["date"];
		$gender = $_POST["gender"];
		$aboutme = $_POST["aboutme"];
		
		$query = "INSERT INTO `user` (`username`, `password`, `nama_lengkap`, `tanggal_lahir`, `email`, `gender`, `avatar`, `about_me`, `achievement`) 
		VALUES 
		('$username', '$password', '$fullname', '$date', '$email', '$gender', 'image/avatar/$username.jpg', '$aboutme', '0')";
		
		$con = mysql_connect("localhost","progin","progin");
		if (!mysql_query($query,$con)){
			die('Error: ' . mysql_error());
		}
		mysql_close($con);

		//upload avatar
		$_FILES["avatar"]["name"] = $_POST["username"].".jpg";
		move_uploaded_file($_FILES["avatar"]["tmp_name"], "image/avatar/" . $_FILES["avatar"]["name"]);
		
		session_start();
		$_SESSION['user'] = $username;
		header("Location:index.php?page=profil&p=1");
		exit();	
	}
}
?>
<?php ob_flush(); ?>